import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {ConfigService} from '@kylin/config';
import {CacheService} from './cache.service';
import {NzMessageService} from 'ng-zorro-antd';
import {CustomRequestParams} from '@kylin/class';
import {RouterService} from '@kylin/router';

@Injectable()

export class HttpService {

    private hostname: string;

    constructor(private http: HttpClient,
                private cacheService: CacheService,
                private httpConfigService: ConfigService,
                private message: NzMessageService,
                private routerService: RouterService
    ) {
        this.hostname = this.httpConfigService.hostname;
    }

    get(path: string, params?: CustomRequestParams): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.get(path, {'params': params}).toPromise()
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    console.warn('err');
                    console.warn(err);
                    reject(err);
                });
        });
    }

    delete(path: string, params?: CustomRequestParams): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.delete(path, {'params': params}).toPromise()
                .then(res => {
                    resolve(res);

                })
                .catch(err => {
                    console.warn('err');
                    console.warn(err);
                    reject(err);
                });
        });
    }

    post(path: string, body: any, params?: CustomRequestParams): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.post(path, body, {'params': params}).toPromise()
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    console.warn('err');
                    console.warn(err);
                    reject(err);
                });
        });
    }

    put(path: string, body?: any, params?: CustomRequestParams): Promise<any> {
        return new Promise((resolve, reject) => {
            this.http.put(path, body, {'params': params}).toPromise()
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    console.warn('err');
                    console.warn(err);
                    reject(err);
                });
        });
    }

    /**
     * 目前只有网络问题导致的HTTP请求无法发出才会走catch
     */
    private handleError() {
        this.cacheService.remove('siderData');
        this.routerService.navigate('/login');
        this.message.warning('您的登陆信息已经失效,请重新登陆!');
    }

}
